package com.lovo.dao.impl;

import com.lovo.dao.BaseDAO;
import com.lovo.dao.UserDAO;
import com.lovo.pojo.UserDO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
// JUnit 单元测试
public class UserDAOImpl extends BaseDAO implements UserDAO {
    @Override
    public UserDO getByUsernameAndPwd(String username, String pwd) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_username=? and b_userpsw=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, username);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con, statement, rs);
        }
        return user;
    }

    @Override
    public void update(UserDO user) {
        // 1. 创建连接
        this.getConnection();
//        statement = null;
        String sql = "update t_bank set b_userpsw=?,b_money=? where b_id=?";
        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getUserpsw());
            statement.setObject(2,user.getMoney());
            statement.setObject(3,user.getId());
            // 3. 执行SQL语句
            int row = statement.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接
            close(con,statement);
        }
    }

    @Override
    public UserDO getById(int id){
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,id);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    @Override
    public UserDO getByUsername(String username) {
        this.getConnection();
        statement = null;
        rs = null;
        UserDO user = null;
        String sql = "select * from t_bank where b_username=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1,username);
            rs = statement.executeQuery();
            if(rs.next()){
                user = new UserDO();
                user.setId(rs.getInt("b_id"));
                user.setUsername(rs.getString("b_username"));
                user.setUserpsw(rs.getString("b_userpsw"));
                user.setMoney(rs.getDouble("b_money"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(con,statement,rs);
        }
        return user;
    }

    public void insert(UserDO user) {
        // 1、 创建链接
        this.getConnection();
        String sql = "INSERT INTO t_bank(b_username,b_userpsw,b_money) values(?,?,?)";
        try {
            // 2、获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1,user.getUsername());
            statement.setObject(2,user.getUserpsw());
            statement.setObject(3,user.getMoney());
            // 3、执行SQL语句
            int row = statement.executeUpdate();
            System.out.println("影响行数：" + row);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4、关闭连接
            close(con, statement);
        }
    }
}

